package com.yinhe.music.yhmusic.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.annotation.Nullable;

import com.yinhe.music.yhmusic.R;
import com.yinhe.music.yhmusic.constants.IConstants;
import com.yinhe.music.yhmusic.log.BlankUtil;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


/**
 * SharedPreferences工具类
 * Created by wcy on 2015/11/28.
 */
public class Preferences {
    private static final String MUSIC_ID = "music_id";
    private static final String PLAY_MODE = "play_mode";
    private static final String SPLASH_URL = "splash_url";
    private static final String NIGHT_MODE = "night_mode";
    private static final String USER_IMAGE = "user_image";
    private static final String DOWNMUSIC_BIT = "down_music_bit";
    private static final String LOGIN_SN = "login_sn";
    private static final String NATION = "nation";
    private static final String IS_PROTECT = "is_protect";
    private static final String IS_VIP = "is_vip";
    private static final String ORDER_NO = "order_no";
    private static final String SEX = "sex";
    private static final String UID = "uid";
    private static final String SEARCH_HISTORY = "search_history";
    private static final String SEARCH_HISTORY_SIZE = "search_history_size";
    private static final String LOCAL_MUSIC_SIZE = "local_music_size";
    private static final String MUSIC_TYPE = "music_type";
    private static final String IS_FIRST = "is_first";
    private static final String FAV_MENU = "fav_menu";
    private static final String QUALITY = "quality";
    private static final String IMAGE_HOST = "image_host";

    private static Context sContext;

    public static void saveSearchHistory(List<String> strList) {
        if (null == strList) {
            return;
        }
        removeHistory();
        int size = strList.size();
        setHistorySize(size);
        for (int i = 0; i < size; i++) {
            saveString(SEARCH_HISTORY + i, strList.get(i));
        }
    }

    public static void saveSearchHistory(String str) {
        int size = getHistorySize();
        saveInt(SEARCH_HISTORY_SIZE, size + 1);
        saveString(SEARCH_HISTORY + (size + 1), str);
    }

    public static List<String> getSearchHistory() {
        List<String> strList = new ArrayList<>();
        int size = getHistorySize();
        for (int i = 0; i < size; i++) {
            strList.add(getString(SEARCH_HISTORY + (i + 1), null));
        }
        Collections.reverse(strList);
        return strList;
    }

    public static void removeHistory() {
        int size = getHistorySize();
        if (0 == size) {
            return;
        }
        remove(SEARCH_HISTORY_SIZE);
        for (int i = 0; i < size; i++) {
            remove(SEARCH_HISTORY + i);
        }
    }

    public static void removeHistoryItem(String str) {
        int size = getInt(SEARCH_HISTORY_SIZE, 0);
        List<String> strList = getSearchHistory();
        if (0 == size) {
            return;
        }

        if (BlankUtil.isBlank(strList) || BlankUtil.isBlank(str)) {
            return;
        }

        List<String> removeList = new ArrayList<>();

        for (int i = 0; i < size; i++) {
            String strTemp = strList.get(i);
            if (str.equals(strTemp)) {
                removeList.add(strList.get(i));
                remove(SEARCH_HISTORY + i);
                saveInt(SEARCH_HISTORY_SIZE, size - 1);
            }
        }
        strList.removeAll(removeList);
        // 删除元素重新建立索引写入数据
        saveSearchHistory(strList);
    }

    public static String getImageHost() {
        return getString(IMAGE_HOST, "");
    }

    public static void setImageHost(String imageHost) {
        saveString(IMAGE_HOST, imageHost);
    }

    public static int getHistorySize() {
        return getInt(SEARCH_HISTORY_SIZE, 0);
    }

    public static void setHistorySize(int size) {
        saveInt(SEARCH_HISTORY_SIZE, size);
    }

    public static int getFavMenu() {
        return getInt(FAV_MENU, 0);
    }

    public static void setFavMenu(int id) {
        saveInt(FAV_MENU, id);
    }

    public static int getLocalMusicSize() {
        return getInt(LOCAL_MUSIC_SIZE, 0);
    }

    public static void setLocalMusicSize(int size) {
        saveInt(LOCAL_MUSIC_SIZE, size);
    }

    public static void init(Context context) {
        sContext = context.getApplicationContext();
    }

    public static long getCurrentSongId() {
        return getLong(MUSIC_ID, -1);
    }

    public static void saveCurrentSongId(long id) {
        saveLong(MUSIC_ID, id);
    }

    public static int getPlayMode() {
        return getInt(PLAY_MODE, 0);
    }

    public static void savePlayMode(int mode) {
        saveInt(PLAY_MODE, mode);
    }

    public static String getSplashUrl() {
        return getString(SPLASH_URL, "");
    }

    public static void saveSplashUrl(String url) {
        saveString(SPLASH_URL, url);
    }

    public static String getQuality() {
        return getString(QUALITY, IConstants.Common);
    }

    public static void setQuality(String quality) {
        saveString(QUALITY, quality);
    }

    public static boolean enableMobileNetworkPlay() {
        return getBoolean(sContext.getString(R.string.setting_key_mobile_network_play), false);
    }

    public static void saveMobileNetworkPlay(boolean enable) {
        saveBoolean(sContext.getString(R.string.setting_key_mobile_network_play), enable);
    }

    public static boolean enableMobileNetworkDownload() {
        return getBoolean(sContext.getString(R.string.setting_key_mobile_network_download), false);
    }

    public static void saveMobileNetworkDownload(boolean enable) {
        saveBoolean(sContext.getString(R.string.setting_key_mobile_network_download), enable);
    }

    public static void setDownMusicBit(int bit) {
        saveInt(DOWNMUSIC_BIT, bit);
    }

    public static String getSex() {
        return getString(SEX, "男");
    }

    public static void setSex(String sex) {
        saveString(SEX, sex);
    }

    public static String getUserImage() {
        return getString(USER_IMAGE, "");
    }

    public static void setUserImage(String image) {
        saveString(USER_IMAGE, image);
    }

    public static String getOrderNo() {
        return getString(ORDER_NO, "");
    }

    public static void setOrderNo(String orderNo) {
        saveString(ORDER_NO, orderNo);
    }


    public static String getLoginSn() {
        return getString(LOGIN_SN, "");
    }

    public static void setNation(String nation) {
        saveString(NATION, nation);
    }

    public static String getNation() {
        return getString(NATION, "藏族");
    }

    public static void setUid(int uid) {
        saveInt(UID, uid);
    }

    public static int getUid() {
        return getInt(UID, 0);
    }

    public static void setLoginSN(String sn) {
        saveString(LOGIN_SN, sn);
    }

    public static Boolean getIsVip() {
        return getBoolean(IS_VIP, false);
    }

    public static void setIsVip(boolean isBuy) {
        saveBoolean(IS_VIP, isBuy);
    }

    public static Boolean getIsProtect() {
        return getBoolean(IS_PROTECT, true);
    }

    public static void setIsProtect(boolean isProtect) {
        saveBoolean(IS_PROTECT, isProtect);
    }

    public static Boolean getIsFirst() {
        return getBoolean(IS_FIRST, true);
    }

    public static void setIsFirst(boolean isFirst) {
        saveBoolean(IS_FIRST, isFirst);
    }

    public static void setMusicType(int type) {
        saveInt(MUSIC_TYPE, type);
    }

    public static int getMusicType() {
        return getInt(MUSIC_TYPE, IConstants.ZANG);
    }

    public static int getDownMusicBit() {
        return getInt(DOWNMUSIC_BIT, 192);
    }


    public static boolean isNightMode() {
        return getBoolean(NIGHT_MODE, false);
    }

    public static void saveNightMode(boolean on) {
        saveBoolean(NIGHT_MODE, on);
    }

    private static boolean getBoolean(String key, boolean defValue) {
        return getPreferences().getBoolean(key, defValue);
    }

    private static void saveBoolean(String key, boolean value) {
        getPreferences().edit().putBoolean(key, value).apply();
    }

    private static int getInt(String key, int defValue) {
        return getPreferences().getInt(key, defValue);
    }

    private static void saveInt(String key, int value) {
        getPreferences().edit().putInt(key, value).apply();
    }

    private static long getLong(String key, long defValue) {
        return getPreferences().getLong(key, defValue);
    }

    private static void saveLong(String key, long value) {
        getPreferences().edit().putLong(key, value).apply();
    }

    private static String getString(String key, @Nullable String defValue) {
        return getPreferences().getString(key, defValue);
    }

    private static void saveString(String key, @Nullable String value) {
        getPreferences().edit().putString(key, value).apply();
    }

    private static SharedPreferences getPreferences() {
        return PreferenceManager.getDefaultSharedPreferences(sContext);
    }

    public static void remove(String key) {
        getPreferences().edit().remove(key).apply();
    }
}
